import redis

def get_connection_pool(dbNum=2,host='127.0.0.1'):
    return redis.ConnectionPool(
                                host = host,
                                port = 6379,
                                password='abc123456',
                                db=dbNum,
                                max_connections=20
                                )

pool = get_connection_pool()
pool_db1 = get_connection_pool(1)


#定义在Redis中操作短信验证码的类
class Redis_SMS_Operator(object):

    def __init__(self,pool_db=pool_db1):
        self.con = redis.Redis(connection_pool=pool_db)

    def add_key_value(self,key,value):
        rest= self.con.set(key,value)
        self.con.expire(key,600)#设置600秒（10分钟）过期
        return rest
    # 删除键值对
    def del_key_value(self,key):
        rest = self.con.delete(key)
        return rest

    def get_key_value(self,key):
        res = self.con.get(key)
        return res

    def __del__(self):
        del self.con


if __name__ == '__main__':
    pool_test = get_connection_pool(1, '127.0.0.1')
    sms_operator = Redis_SMS_Operator(pool_test)
    res_add = sms_operator.add_key_value('18308576111', '123456')
    res_get = sms_operator.get_key_value('18308576111')
    print(res_get is not None)